package com.extrace;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import com.extrace.loader.PackageHistoryLoader;
import com.extrace.model.ExpressSheet;
import com.extrace.model.PackageHistory;
import com.extrace.model.TransHistory;
import com.extrace.model.TransPackage;
import com.extrace.model.TransPackageContent;
import com.extrace.net.IDataAdapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity implements IDataAdapter<PackageHistory>{

    private EditText ExpressId;
    private Button btnByList;
    private Button btnByMap;
    private ListView ExHistoryList;
    private PackageHistoryLoader mLoader;
    ExHistoryListAdapter mAdapter;

    private Intent intent;
    private List<PackageHistory> phList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ExpressId = (EditText)findViewById(R.id.expressId);

        btnByList = (Button) findViewById(R.id.btnByList);
        btnByList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ListLoader(ExpressId.getText().toString());
                //AdapterNew(phList);
            }
        });

        btnByMap = (Button) findViewById(R.id.btnByMap);
        btnByMap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,Map_Activity.class);//**intent定义类型，没见过
                intent.putExtra("expressId", ExpressId.getText().toString());
                startActivity(intent);//这两行代码没见过？？是跳转另一个activity的方法
                Toast.makeText(MainActivity.this, "转到地图", Toast.LENGTH_SHORT).show();
            }
        });

        ExHistoryList = (ListView) findViewById(R.id.transHistory);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case RESULT_OK:
                intent = this.getIntent();
                if (intent.hasExtra("expressId"))
                    ListLoader(intent.getStringExtra("expressId"));
        }
    }

    public void AdapterNew(List<PackageHistory> list) {
        mAdapter = new ExHistoryListAdapter(getApplicationContext(), R.layout.transhistory_list_item,list);
        ExHistoryList.setAdapter(mAdapter);
    }

    public void ListLoader(String id) {
        try {
            mLoader = new PackageHistoryLoader(this, this);
            mLoader.getTPC(id);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public List<PackageHistory> getHistory(List<TransPackageContent> list) {
        List<PackageHistory> list1 = new ArrayList<PackageHistory>();
        for (TransPackageContent tpc : list) {
            PackageHistory ph = new PackageHistory();
            switch (tpc.getPkg().getStatus()) {
                case TransPackage.STATUS.STATUS_LANSHOU_PACKAGE:
                    ph.setTime(tpc.getExpress().getAccepteTime());
                    ph.setPkAction("快件已揽收！");
                    ph.setStatus(tpc.getPkg().getTargetNode().getNodeName());
                    list1.add(ph);
                    break;
                case TransPackage.STATUS.STATUS_DESTROY:
                    ph.setTime(tpc.getPkg().getCreateTime());
                    ph.setPkAction("快件已打包！");
                    ph.setStatus(tpc.getPkg().getSourceNode().getNodeName());
                    list1.add(ph);
                    getHistoryByPackageId(tpc.getPkg().getID());
                    break;
                case TransPackage.STATUS.STATUS_DELIVER_PACKAGE:
                    ph.setTime(tpc.getExpress().getDeliveTime());
                    ph.setPkAction("快件派送中！");
                    ph.setStatus(tpc.getPkg().getTargetNode().getNodeName());
                    list1.add(ph);
                    ph = new PackageHistory();
                    if (tpc.getExpress().getStatus() == ExpressSheet.STATUS.STATUS_DELIVERY)
                    {
                        ph.setTime(tpc.getExpress().getDeliveTime());
                        ph.setPkAction("快件已签收！");
                        ph.setStatus("          ");
                        list1.add(ph);
                    }
                    break;
                case TransPackage.STATUS.STATUS_TRANSPORT:
                    ph.setTime(tpc.getPkg().getCreateTime());
                    ph.setPkAction("快件已打包！");
                    ph.setStatus(tpc.getPkg().getSourceNode().getNodeName());
                    list1.add(ph);
                    ph = new PackageHistory();
                    ph.setTime(getCurrentDate());
                    ph.setPkAction("快件转运中！");
                    ph.setStatus(tpc.getPkg().getSourceNode().getNodeName());
                    list1.add(ph);
                    break;
                case TransPackage.STATUS.STATUS_PACKAGING:
                    ph.setTime(getCurrentDate());
                    ph.setPkAction("快件打包中！");
                    ph.setStatus(tpc.getPkg().getSourceNode().getNodeName());
                    list1.add(ph);
                    break;
            }
        }
        return list1;
    }

    public Date getCurrentDate() {
        //产生一个不带毫秒的时间,不然,SQL时间和JAVA时间格式不一致
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date tm = new Date();
        try {
            tm= sdf.parse(sdf.format(new Date()));
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
        return tm;
    }

    public void getHistoryByPackageId(String id) {
        try {
            mLoader = new PackageHistoryLoader(this, this);
            mLoader.getHistoryByPkgId(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public PackageHistory getData() {
        return null;
    }

    @Override
    public void setData(PackageHistory data) {

    }

    @Override
    public void notifyDataSetChanged() {
        TransHistory th = mLoader.th;
        if (th != null) {
            int i = 0;
            Date date = th.getActTime();
            for (PackageHistory ph : phList) {
                if (ph.getTime().after(date))
                {
                    PackageHistory packageHistory = new PackageHistory();
                    packageHistory.setTime(date);
                    packageHistory.setPkAction("快件到达"+ph.getStatus());
                    packageHistory.setStatus("打包处理人："+th.getUIDFrom().getName()+";拆包处理人：" + th.getUIDTo().getName());
                    phList.add(i, packageHistory);
                    break;
                }
                i++;
            }
        }
        else {
            List<TransPackageContent> list = mLoader.list;
            phList = getHistory(list);
        }
        AdapterNew(phList);
    }
}